package com.chenjun.common.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 日期转换工具类
 * 
 * @Title DateUtil
 * @author 李伟
 * @date 2015年6月24日 下午1:37:53
 */
public class DateUtil {

	private static final Logger logger = Logger.getLogger(DateUtil.class);

	/**
	 * yyyy-MM
	 */
	public static final String DATE_SHORT_PATTERN_FYM = "yyyy-MM";

	/**
	 * yyyy-MM-dd
	 */
	public static final String DATE_SHORT_PATTERN = "yyyy-MM-dd";
	/**
	 * yyyy-MM-dd HH:mm
	 */
	public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm";
	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
	/**
	 * HH:mm
	 */
	public static final String TIME_SHORT_PALLTTRN = "HH:mm";
	/**
	 * HH:mm:ss
	 */
	public static final String TIME_PATTERN = "HH:mm:ss";
	/**
	 * ddMMMyyyy
	 */
	public static final String TIME_PATTERN_EN = "ddMMMyyyy";
	/**
	 * dd/MM/yyyy
	 */
	private static final String TIME_PATTERN_EN2 = "dd/MM/yyyy";
	/**
	 * yyyyMMddHHmmss
	 */
	private static final String LONG_DATE_PATTERN = "yyyyMMddHHmmss";

	private static final String yyyyMMdd = "yyyyMMdd";

	/**
	 * yyyyMMddHHmmss
	 */
	private static final String LONG_DATE_PATTERNSSS = "yyyyMMddHHmmssSSS";
	/**
	 * yyyy年MM月dd日
	 */
	public static final String DATE_SHORT = "yyyy年MM月dd日";

	/**
	 * (yyyy)年(MM)月(dd)日
	 */
	public static final String BUSINESS_EMAIL_DATE = "(yyyy)年(MM)月(dd)日";

	public static final String ENGLISH_DATE_SHORT = "d MMM yyyy";

	/**
	 * 获取指定日期的长格式化类型
	 * 
	 * @param date
	 *            : 为空默认为当前日期
	 * @return String
	 */
	public static String nowDateStr(Date date) {
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(LONG_DATE_PATTERN);
		return format.format(date);
	}

	public static String nowDateStrSSS(Date date) {
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(LONG_DATE_PATTERNSSS);
		return format.format(date);
	}

	public static String nowDateStryyyyMMdd(Date date) {
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(yyyyMMdd);
		return format.format(date);
	}

	public static String nowDate(Date date) {
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
		return format.format(date);
	}

	/**
	 * 获得当前日期
	 * 
	 * @return
	 */
	public static Timestamp newDate() {
		SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
		String timesampDate = format.format(new Date());
		return Timestamp.valueOf(timesampDate);
	}

	public static String dateToStringEn2(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN_EN2);
		return sdf.format(date);
	}

	/**
	 * 将时间格式时间转换为字符串 HH:mm:ss
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToLongString(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(TIME_PATTERN);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 将时间格式时间转换为字符串 HH:mm
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToShortTime(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(TIME_SHORT_PALLTTRN);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 将字符串转换为时间格式 HH:mm
	 * 
	 * @param dateDate
	 * @return
	 */
	public static Date strDateToShortTime(String date) {
		SimpleDateFormat format = new SimpleDateFormat(TIME_SHORT_PALLTTRN);
		Date d = null;
		try {
			d = format.parse(date);
		} catch (ParseException e) {
			logger.error(e);
		}
		return d;
	}

	/**
	 * 将时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateTimeToStrLong(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 将时间格式时间转换为字符串 yyyy-MM-dd HH:mm
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateTimeToStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_PATTERN);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 将时间格式时间转换为字符串 yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToStrShort(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_SHORT_PATTERN);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 将时间格式时间转换为字符串 d MMM yyyy
	 * 
	 * @param date
	 * @return
	 */
	public static String endateToStrShort(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(ENGLISH_DATE_SHORT, Locale.ENGLISH);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 将字符串转换为时间格式时间 yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static Date strToDateShort(String str) {
		SimpleDateFormat fmt = new SimpleDateFormat(DATE_SHORT_PATTERN);
		Date date;
		try {
			date = fmt.parse(str);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return null;
	}

	/**
	 * 将时间格式时间转换为字符串 yyyy-MM
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToStrShortym(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_SHORT_PATTERN_FYM);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 将时间格式时间转换为字符串 yyyy年MM月dd日
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToChinaStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_SHORT);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * 
	 * 将时间格式时间转换为字符串 (yyyy)年(MM)月(dd)日
	 * 
	 * @param date
	 * @return String
	 */
	public static String dateToBusinessStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(BUSINESS_EMAIL_DATE);
		String timesampDate = format.format(date);
		return timesampDate;
	}

	/**
	 * Date转Timestamp
	 * 
	 * @param date
	 * @return
	 */
	public static Timestamp dateToTimestamp(Date date) {
		return new Timestamp(date.getTime());
	}

	/**
	 * 将timestamp转换成date
	 * 
	 * @param timestamp
	 * @return
	 */
	public static Date timestampToDate(Timestamp timestamp) {
		return new Date(timestamp.getTime());
	}

	/**
	 * 将指定日期与指定格式转换成字符串形式
	 * 
	 * @param date
	 *            ---被转换日期
	 * @param strInPattern
	 *            ----日期样式
	 * @return
	 */
	public static String dateToStr(Date date, String strInPattern) {
		try {
			if (null == date || StringUtils.isBlank(strInPattern)) {
				return "";
			} else {
				SimpleDateFormat sdf = new SimpleDateFormat(strInPattern);
				return sdf.format(date);
			}
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 将String类型的日期转成Date
	 */
	public static Date parseShotStringToDate(String stringDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String strDate = stringDate;
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将长String类型日期转成Date:yyyy-MM-dd HH:mm
	 * 
	 * @param stringDate
	 * @return Date
	 */
	public static Date parseStringToDate(String stringDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
		String strDate = stringDate;
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将长String类型的日期转成Date：yyyy-MM-dd HH:mm:ss
	 */
	public static Date parseLongStringToDate(String stringDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		String strDate = stringDate;
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将长String类型的日期转成Date：yyyy-MM-dd
	 */
	public static Date parseLongStringToDateYYYYMMDD(String stringDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_SHORT_PATTERN);
		String strDate = stringDate;
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 日期加一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date addOneDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, 1);
		return c.getTime();
	}

	public static Date getBeforeDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, -1);
		return c.getTime();
	}

	public static Date getAfterDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, 1);
		return c.getTime();
	}

	/**
	 * 日期累加天数返回
	 * 
	 * @param date
	 *            日期，为空当取当前时间
	 * @param addDays
	 *            要累加的天数
	 * @return
	 */
	public static Date addDay(Date date, int addedDays) {
		Calendar c = Calendar.getInstance();
		if (date == null) {
			c.setTime(new Date());
		} else {
			c.setTime(date);
		}
		addedDays = ((addedDays <= 0) ? 1 : addedDays);
		c.add(Calendar.DAY_OF_YEAR, addedDays);
		return c.getTime();
	}

	/**
	 * 日期累加天数返回,如果是负值则向前移动相应天数
	 * 
	 * @param date
	 * @param addedDays
	 * @return
	 */
	public static Date addOrLessDay(Date date, int addedDays) {
		Calendar c = Calendar.getInstance();
		if (date == null) {
			c.setTime(new Date());
		} else {
			c.setTime(date);
		}
		c.add(Calendar.DAY_OF_YEAR, addedDays);
		return c.getTime();
	}

	/**
	 * 时间转换
	 * */
	public static String timestamp2String11(Timestamp arg) {
		if (arg == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(arg);
	}

	/**
	 * 日期加整数天
	 * 
	 * @param date
	 * @param Day
	 * @return
	 */
	public static Date setDay(Date date, int Day) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, Day);
		return c.getTime();
	}

	/**
	 * 日期减一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date minusOneDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, -1);
		return c.getTime();
	}

	/**
	 * <p>
	 * Description: 日期减一年减一天
	 * </p>
	 * 
	 * @param date
	 * @return
	 */
	public static Date minusYearAddDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, -1);
		c.add(Calendar.DATE, -1);
		return c.getTime();
	}

	/**
	 * <p>
	 * Description: 日期减N年减N月减N天
	 * </p>
	 * <p>
	 * 不增不减参数传递0
	 * </p>
	 * <p>
	 * 例如：减一天传递-1，增加一天传递1
	 * </p>
	 * 
	 * @param date
	 * @return
	 */
	public static Date adjustYearAndMonthAndDay(Date date, int year, int month, int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, year);
		c.add(Calendar.MONTH, month);
		c.add(Calendar.DATE, day);
		return c.getTime();
	}

	/**
	 * <p>
	 * Description: 日期减一个月
	 * </p>
	 * 
	 * @param date
	 * @return
	 */
	public static Date minusMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, -1);
		return c.getTime();
	}

	/**
	 * 增加多少个小时后的日期
	 * 
	 * @param date
	 * @param hours
	 * @return
	 */
	public static Date addHoursDate(Date date, int hours) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR, hours);
		return c.getTime();
	}

	/**
	 * 增减分钟后的日期
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date addMinuteDate(Date date, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, minute);
		return c.getTime();
	}

	public static Date getDate(int year, int month, int days, int hour, int minute, int seconds) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		if (year > 0) {
			calendar.set(Calendar.YEAR, year);
		}
		if (month != 0) {
			calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + month);
		}

		if (days != 0) {
			calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + days);
		}

		if (hour > 0) {
			calendar.set(Calendar.HOUR_OF_DAY, hour);
		}

		if (minute > 0) {
			calendar.set(Calendar.MINUTE, minute);
		} else {
			calendar.set(Calendar.MINUTE, 0);
		}

		if (seconds > 0) {
			calendar.set(Calendar.SECOND, seconds);
		} else {
			calendar.set(Calendar.SECOND, 0);
		}
		return calendar.getTime();
	}

	/**
	 * 获得当前时间
	 * */
	public static Timestamp getCurrent() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static Date getDate(Date cDate, int year, int month, int days, int hour, int minute, int seconds) {
		Calendar calendar = Calendar.getInstance();
		if (null != cDate) {
			calendar.setTime(cDate);
		} else {
			calendar.setTime(new Date());
		}

		if (year > 0) {
			calendar.set(Calendar.YEAR, year);
		}

		if (month > 0) {
			calendar.set(Calendar.MONTH, month);
		}

		if (days != 0) {
			calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + days);
		}

		if (hour >= 0) {
			calendar.set(Calendar.HOUR_OF_DAY, hour);
		}

		if (minute >= 0) {
			calendar.set(Calendar.MINUTE, minute);
		}

		if (seconds >= 0) {
			calendar.set(Calendar.SECOND, seconds);
		}

		return calendar.getTime();
	}

	/**
	 * 12月3号7点到12月4号7点之间的时间段，返回3号的日期。。然后按需处理
	 * 
	 * @return
	 */
	public static Date getWorkDate() {
		Calendar rightNow = Calendar.getInstance();
		int hour = rightNow.get(Calendar.HOUR_OF_DAY);
		int minute = rightNow.get(Calendar.MINUTE);
		if (hour < 7 || (hour == 7 && minute == 0)) {
			rightNow.add(Calendar.DATE, -1);
		}
		return rightNow.getTime();
	}

	/**
	 * 获取当前日期是星期几 0:礼拜天 1~6:礼拜一~礼拜六
	 * 
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static int getWeekOfDate(Date dt) {
		Calendar cal = Calendar.getInstance();
		if (null == dt) {
			dt = new Date();
		}
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return w;
	}

	/**
	 * 获取当前是几月
	 * 
	 * @param dt
	 * @return
	 */
	public static int getMonth(Date dt) {
		Calendar cal = Calendar.getInstance();
		if (null == dt) {
			dt = new Date();
		}
		cal.setTime(dt);
		int w = cal.get(Calendar.MONTH);
		if (w < 0)
			w = 0;
		return w;
	}

	/**
	 * 获取当前日期是几号
	 * 
	 * @param dt
	 * @return
	 */
	public static int getDayOfMonth(Date dt) {
		Calendar cal = Calendar.getInstance();
		if (null == dt) {
			dt = new Date();
		}
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_MONTH);
		if (w < 0)
			w = 0;
		return w;
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @see daysDateBetween
	 * @param smdate较小的时间
	 * @param bdate较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(String smdate, String bdate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return daysDateBetween(sdf.parse(smdate), sdf.parse(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 计算两个日期之间相差的天数 "2014-06-22", "2014-06-23"相差一天
	 * 
	 * @param smdate较小的时间
	 * @param bdate较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysDateBetween(Date smdate, Date bdate) {
		Calendar cal = Calendar.getInstance();
		long between_days = 0;
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 获得两个月相差多少月
	 * */
	public static int compareDate(String date1, String date2) {
		int n = 0;

		String formatStyle = "yyyy-MM";

		date2 = date2 == null ? DateUtil.dateToStrShort(new Date()) : date2;

		SimpleDateFormat df = new SimpleDateFormat(formatStyle);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(date1));
			c2.setTime(df.parse(date2));
		} catch (Exception e3) {
			System.out.println("wrong occured");
		}
		while (!c1.after(c2)) { // 循环对比，直到相等，n 就是所要的结果
			n++;
			c1.add(Calendar.MONTH, 1); // 比较月份，月份+1

		}
		n = n - 1;
		return n;
	}

	/**
	 * 获取某月某天
	 * 
	 * @return
	 */
	public static Date getMonthDay(Date cDate, int month, int day) {
		Calendar cal = Calendar.getInstance();
		if (null != cDate) {
			cal.setTime(cDate);
		} else {
			cal.setTime(new Date());
		}
		cal.add(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}

	/**
	 * 计算两个日期之间相差的天时分秒
	 * 
	 * @param smdate较小的时间
	 * @param bdate较大的时间
	 * @return 相差的HH:mm:ss
	 * @throws ParseException
	 */
	public static String timesBetweenDate(Date smdate, Date bdate) {
		Calendar cal = Calendar.getInstance();
		if (null == smdate) {
			smdate = new Date();
		}
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();

		if (null == bdate) {
			bdate = new Date();
		}
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();

		long time = (time2 - time1) / 1000;
		long dd = 0;
		long HH = 0;
		long mm = 0;
		long ss = 0;
		if (time > 0) {
			dd = time / (3600 * 24);
			HH = time % (3600 * 24) / 3600;
			mm = time % (3600 * 24) % 3600 / 60;
			ss = time % (3600 * 24) % 3600 % 60;
		}

		String betweenTime = String.valueOf(dd) + ":" + String.valueOf(HH) + ":" + String.valueOf(mm) + ":"
				+ String.valueOf(ss);

		return betweenTime;
	}

	public static String getMonthRef(int month) {
		String rtRef = "";
		switch (month) {
		case 0:
			rtRef = "一";
			break;
		case 1:
			rtRef = "二";
			break;
		case 2:
			rtRef = "三";
			break;
		case 3:
			rtRef = "四";
			break;
		case 4:
			rtRef = "五";
			break;
		case 5:
			rtRef = "六";
			break;
		case 6:
			rtRef = "七";
			break;
		case 7:
			rtRef = "八";
			break;
		case 8:
			rtRef = "九";
			break;
		case 9:
			rtRef = "十";
			break;
		case 10:
			rtRef = "十一";
			break;
		case 11:
			rtRef = "十二";
			break;
		default:
			break;
		}
		return rtRef;
	}

	/**
	 * 
	 * 获取当前日期的几天前的日期
	 * 
	 * @author liuzhifu
	 * @date 2015年8月24日 上午10:19:44
	 * @return
	 */
	public static String getBeforDate(int day) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_SHORT_PATTERN);
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -day);
		Date monday = c.getTime();
		String preMonday = sdf.format(monday);
		return preMonday;
	}

	/**
	 * 
	 * 获取当前日期的几天前的日期,返回数组,[0]为几天前日期,[1]为现在日期
	 * 
	 * @author huanghao
	 * @date 2015年8月24日 上午10:19:44
	 * @return
	 */
	public static String[] getNowAndBeforDate(int day) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		Calendar calendar = new GregorianCalendar();
		String dateStrNow = sdf.format(calendar.getTime());
		calendar.add(Calendar.DATE, -day);
		String dateStrLast = sdf.format(calendar.getTime());
		String[] str = new String[2];
		str[0] = dateStrLast;
		str[1] = dateStrNow;
		return str;
	}

	/**
	 * 
	 * 获取当前日期的几天前的日期,返回数组,[0]为几天前日期,[1]为现在日期
	 * 
	 * @author huanghao
	 * @date 2015年8月24日 上午10:19:44
	 * @return
	 * @throws ParseException
	 */
	public static String getAllBetweenStAndEnd(String startDateold, String endDateold) throws ParseException {
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat(DATE_SHORT_PATTERN);
		Date startDate = df.parse(startDateold);
		startCalendar.setTime(startDate);
		Date endDate = df.parse(endDateold);
		endCalendar.setTime(endDate);
		String x = "";
		x += startDateold;
		while (true) {
			startCalendar.add(Calendar.DAY_OF_MONTH, 1);
			if (startCalendar.getTimeInMillis() < endCalendar.getTimeInMillis()) {// TODO
																					// 转数组或是集合，楼主看着写吧
				// System.out.println(df.format(startCalendar.getTime()));
				if (!x.equals("")) {
					x += ",";
				}
				x += df.format(startCalendar.getTime());

			} else {
				break;
			}
		}
		x += "," + endDateold;
		return x;
	}

	/**
	 * 
	 * 获取当前日期的几天前的日期,返回数组,[0]为几天前日期,[1]为现在日期
	 * 
	 * @author huanghao
	 * @date 2015年8月24日 上午10:19:44
	 * @return
	 * @throws ParseException
	 */
	public static String getweekByNum(String num) throws ParseException {
		String x = "";
		if (num.equals("1")) {
			x = "周一";
		}
		if (num.equals("2")) {
			x = "周二";
		}
		if (num.equals("3")) {
			x = "周三";
		}
		if (num.equals("4")) {
			x = "周四";
		}
		if (num.equals("5")) {
			x = "周五";
		}
		if (num.equals("6")) {
			x = "周六";
		}
		if (num.equals("7")) {
			x = "周日";
		}
		return x;
	}

	// 根据日期获取周几
	public static List<Map<String, Object>> getWeekOfByDate(String date) throws ParseException {
		List<Map<String, Object>> datelist = new ArrayList<Map<String, Object>>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] dates = date.split(",");
		if (dates.length > 0) {
			for (int i = 0; i < dates.length; i++) {
				Map<String, Object> datemap = new HashMap<String, Object>();
				String[] weekDaysName = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
				String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(sdf.parse(dates[i]));
				int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
				String week = weekDaysName[Integer.parseInt(weekDaysCode[intWeek])];
				datemap.put("weekday", week);
				datemap.put("weekDate", dates[i]);
				datelist.add(datemap);
			}
		}

		return datelist;
	}

	/**
	 * 根据开始时间和结束时间返回时间段内的时间集合
	 * 
	 * @author fanjl
	 * @param beginDate
	 * @param endDate
	 * @return List
	 */
	public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
		List<Date> lDate = new ArrayList<Date>();
		lDate.add(beginDate);// 把开始时间加入集合
		Calendar cal = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		cal.setTime(beginDate);
		boolean bContinue = true;
		while (bContinue) {
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			cal.add(Calendar.DAY_OF_MONTH, 1);
			// 测试此日期是否在指定日期之后
			if (endDate.after(cal.getTime())) {
				lDate.add(cal.getTime());
			} else {
				break;
			}
		}
		lDate.add(endDate);// 把结束时间加入集合
		return lDate;
	}

	/**
	 * 取得日期的年份，格式"yyyy"
	 * 
	 * @param date
	 *            日期
	 * @return 日期的年份
	 */
	public static int getYear(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		return Integer.parseInt(formatter.format(date));
	}

	/**
	 * @Description:获取两个时间相差小时数
	 * @param startTime
	 * @param endTime
	 * @return
	 * @author dongyibin
	 * @date 2017-8-27 上午11:55:48
	 */
	public static BigDecimal getDifferenceHour(long startTime, long endTime) {
		BigDecimal b_diffTime = new BigDecimal(endTime - startTime);
		BigDecimal b_oneHours = new BigDecimal(60 * 60 * 1000);
		return b_diffTime.divide(b_oneHours, 1, BigDecimal.ROUND_HALF_UP); // 保留一位小数四舍五入
	}

	public static void main(String[] args) {
		// System.out.println(parseLongStringToDate("2015-08-24 00:00:00"));
		// System.out.println(addHoursDate(new Date(),1));
		// System.out.println(nowDate(new Date(1449654884925l)));
		// System.out.println(nowDate(new Date(1449654521272l)));
		// System.out.println(nowDate(new Date(1449654778514l)));
		// System.out.println(nowDate(new Date(1449654584373l)));
		String now = "2015-01-5";
		String a = "2015-01-15";
		System.out.println(now.compareTo(a) == 0);
		System.out.println(daysBetween(now, a));

	}

	/**
	 * @Description: 判断日期是否为周末
	 * @param calendar
	 * @return
	 * @author dongyibin
	 * @date 2017-8-30 下午7:33:23
	 */
	public static boolean isWeekend(Calendar calendar) {
		if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
				|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
			return true;
		}
		return false;
	}
}
